package mainpackage;

import java.util.*;
import java.awt.event.ActionListener;

public class ObservableHashMap<K, V> extends LinkedHashMap<K, V> {

    private static final long serialVersionUID = -6491765759436880133L;

    private Vector<Object> monitor_observers;
    Iterator observersIterator;

    private int maxCapacity;

    @Override
    public synchronized V put(K key, V value) {

        V result = super.put(key, value);
            observersIterator = monitor_observers.iterator();
            while(observersIterator.hasNext()){
                Object obj = observersIterator.next();
                synchronized (obj) {
                    obj.notifyAll();
                }
            }
        return result;
    }

    public void addMonitor(Object monitor) {
        if(monitor == null)
            throw new NullPointerException();
        this.monitor_observers.add(monitor);
    }

    protected boolean removeEldestEntry(Map.Entry<K,V> eldest){
        return size() > maxCapacity;
    }

    public ObservableHashMap(int maxCapacity){
        this.maxCapacity = maxCapacity;
        monitor_observers = new Vector();
    }
}
